Repository navigation
Benchmarks: the copy-out collectors against a growing list, a builder and a stack scratch - #288
Merged
Merged
Conversation
… and a stack scratch A new BenchmarkDotNet class family (`CopyOutInt`, `CopyOutSmallStruct`, `CopyOutClass`, one per row kind: an `int`, a 16-byte struct, a class with a string) for the question "does the chunk chain with its clear lose to the hand-written ways at small counts?". Over an in-memory `DataTableReader` (no database), at 0, 1, 5, 10, 100, 1,000 and 100,000 rows (`ODX_COPYOUT_ROWS` changes them), every variant through the same selector delegate, synchronous and asynchronous, each result returned so the harness consumes it, with the memory diagnoser: - the library's `ToList`, `ToArray` and `ToImmutableArray` (chain plus clear); - a `List<T>` that grows, that list then `ToArray()`, and `ImmutableArray.CreateBuilder<T>()` of unknown capacity then `ToImmutable()`; - a prototype, in the benchmark file only, of a stack scratch buffer of 8 rows in front of the chain (synchronous only; a ref struct cannot live across an await), modelled on `System.Linq`'s internal segmented builder, so the timed run can say whether it is worth adopting. `--verify-copyout` checks, without BenchmarkDotNet, that every variant gives the same rows as the growing list for each row kind and counts around the scratch and chunk edges, and that the chain's lists are exactly sized. The results are grouped by sync and async and by row count, each group against its own growing-list baseline; the hand-written async variants return `ValueTask`, as the library's do, so no `Task` is allocated on one side only. Not run as timed benchmarks by this change: that needs a quiet machine. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…out of the comparison The chain variants went through `reader.Map(selector).ToList()`, the library's terminal path (the `Map` view, the abandon bookkeeping, the plumbing), while the growing list, the builder and the scratch used a raw read loop, so the comparison mixed the terminal overhead with the collection strategy. Added `Sync_RawChain_ToList`, `ToArray` and `ToImmutableArray` and `Async_RawChain_ToList`: the same read loop as the growing list, rows into the real chunk chain with the default configuration (copy-out clearing on), then the chain's own `ToList`, `ToArray` or the immutable build. The library variants stay, as they are what a user gets; the difference between a library variant and its raw-chain variant is the terminal's cost. The benchmark assembly now sees Core's internals (`InternalsVisibleTo`, one line in the Core project, with its comment); nothing public changes. `--verify-copyout` covers the new variants, including the exact capacity of their lists. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A BenchmarkDotNet class family for the copy-out collectors, to find any small-count loss of the chunk chain with its clear against the hand-written ways. Build-only here: nothing was run as a timed benchmark (that needs a quiet machine), and no BenchmarkDotNet run was made.
What it measures
CopyOutInt,CopyOutSmallStruct(a 16-byte struct) andCopyOutClass(a class with a string), each over an in-memoryDataTableReader(no database) at 0, 1, 5, 10, 100, 1,000 and 100,000 rows (ODX_COPYOUT_ROWSchanges them). Every variant reads through the same selector delegate and returns its collection, so the harness consumes it; the memory diagnoser is on.ToList,ToArray,ToImmutableArray(chunk chain, one copy, chunks cleared)Mapview and none of the terminal's plumbing (RawChain; async:ToListonly)ToListList<T>that grows (the baseline of its group)ToArray()ImmutableArray.CreateBuilder<T>()of unknown capacity, thenToImmutable()Results are grouped by sync and async and by row count, each group against its own growing-list baseline. The hand-written async variants return
ValueTask, as the library's do, so noTaskis allocated on one side only. What the table cannot hide, and the report should say: the chain and the scratch rent and clear pooled chunks, the hand-written ones do neither. The library variants answer "what users get"; the raw-chain variants use the same read loop as the growing list, so against it they isolate the collection strategy, and a library variant minus its raw-chain variant is what the terminal path itself costs (theMapview, the abandon bookkeeping).The scratch variant is synchronous only: a ref struct cannot live across an await. It uses the library's own chunk schedule (16 doubling to 8,192) and clears the used part of every segment on the way back (the copy-out rule), and holds at most 27 segments (about 155,000 rows; more throws), as the rows stop at 100,000.
System.Linq's segmented builder, for the scratch variant
Read in
dotnet/runtime,src/libraries/System.Linq/src/System/Linq/SegmentedArrayBuilder.cs, at the tagsv10.0.15(the latest release) andv11.0.0; the file is identical at both.Enumerable.ToArrayof a source of unknown length uses it (ToCollection.cs, same tags). No code is copied.ArrayPool<T>.Shared, at least 16 elements and at least double the previous one, held in an inline array of 27 slots.ScratchBufferSize = 8, anInlineArray(8)local); the first 8 items go there and nothing is rented until the ninth.The raw-chain variants need the benchmark assembly to see Core's internals: one
InternalsVisibleToline in the Core project (owner-approved), with its comment. Nothing public changes.The check
--verify-copyout(no BenchmarkDotNet, nothing measured) runs every variant for each row kind at 0, 1, 5, 8, 9, 10, 16, 24, 25, 100, 1,000, 8,200 and 100,000 rows (the new raw-chain variants included), twice each (the second call reuses the pooled arrays of the first), and compares the rows with the growing list, and the exact capacity of the chain's lists. On this branch: "copy-out variants: all equal".Checks
dotnet format styleandanalyzersat--severity inforeport nothing on all 14 projects; the docs build is clean (a benchmark-only change, run through the whole gate anyway).Taskthe library's do not; one baseline across sync and async made the async ratios misleading; the scratch prototype overran its 27 slots past about 155,000 rows and kept state afterDispose; the capacity check skipped the scratch list. Dropped: a claim that the inline array does not convert to a span (it compiles and the check runs).🤖 Generated with Claude Code